package sensors;

import flixel.group.FlxTypedGroup;
import flixel.addons.tile.FlxTilemapExt;
import flixel.FlxG;
import flixel.tweens.FlxEase;
import level.Tiles;


class DoorSensor extends DependentSensor
{
	//public var id: Int;						// id del sensor

	private var _doorIsOpen: Bool;			// si la puerta está o no abierta
	private var _openPermanent: Bool;		// si una vez abierta la puerta debe quedarse así para el resto del juego
	private var _isOpening: Bool = false;	
	private var _openingElapsed: Float;
	private static inline var DOOR_OPEN_TIME: Float = 1;
	private static var DOOR_OPEN_EASING = FlxEase.cubeIn;
	private var _solidMap: FlxTilemapExt;	// las puertas son almacenadas en la capa solida

	// constructor de la puerta, 
	public function new(X: Float, Y: Float, map: FlxTilemapExt, solidMap: FlxTilemapExt, PermanentOpen: Bool = true){
 		super(X, Y, map);
 		_solidMap = solidMap;
 		_doorIsOpen = false;
 		_openPermanent = PermanentOpen;
 		deactivate();
 		BasicSensor._sensors.get("DoorSensor").add(this);
 		// si está abierta de manera permanente, abrir
 		if (Reg.hasItem("door_"+Reg.levelName+"_"+_iTile))
 		{
 			open_noAnimate();
 		}
	}

	public override function update()
	{
		super.update();
		
		if (_parents.length > 0 && _parents[0].isActive)
		{	
			if (!isActive)
			{
				activate();
			}
		}else{
			deactivate();
		}

		if (_isOpening)
		{
			_openingElapsed += FlxG.elapsed;
			drawDoor(DOOR_OPEN_EASING(_openingElapsed/DOOR_OPEN_TIME));	
		}
	}

	override public function activate()
	{
		super.activate();
		_map.setTileByIndex(_iTile, Tiles.DOOR_SENSOR_ON, true);
		if (!_doorIsOpen && !_isOpening)
		{
			startAnimatedOpen();
			if(_openPermanent)
			{
				Reg.setItem("door_"+Reg.levelName+"_"+_iTile, 1);
			}
		}
		
	}

	override public function deactivate()
	{
		if (isActive)
		{
			super.deactivate();
			_map.setTileByIndex(_iTile, Tiles.DOOR_SENSOR_OFF, true);
		}
	}

	private function startAnimatedOpen()
	{
		_isOpening = true;
		_openingElapsed = 0;
	}

	private function open_noAnimate()
	{
		if (!_doorIsOpen)
		{
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles, 0, true);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles*2, 0, true);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles*3, 0, true);
			_doorIsOpen = true;
			if (_openPermanent)
			{
				Reg.setItem("door_"+id, 1);
			}
		}
	}

	private function drawDoor(f: Float)
	{ 
		if (f <= 0.3333) {
			var t: Float = f/0.3333;
			var tile = Math.round(t*7);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles, Tiles.DOOR_TILE, true);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles*2, Tiles.DOOR_TILE, true);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles*3, Tiles.DOOR_TILE+tile, true);
		}
		else if (f>0.3333 && f<=0.6666)
		{
			var t: Float = (f-0.3333)/(0.6666-0.3333);
			var tile = Math.round(t*7);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles, Tiles.DOOR_TILE, true);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles*2, Tiles.DOOR_TILE+tile, true);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles*3, 0, true);
		}
		else if (f>0.6666 && f<1)
		{
			var t: Float = (f-0.6666)/(1-0.6666);
			var tile = Math.round(t*7);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles, Tiles.DOOR_TILE+tile, true);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles*2, 0, true);
			_solidMap.setTileByIndex(_iTile+_solidMap.widthInTiles*3, 0, true);
		}
		else
		{
			open_noAnimate();
			_isOpening = false;
			_doorIsOpen = true;
		}
	}
}